package gen.tools.test;

import gen.tools.Widgets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ChkTestResult extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ChkTestResult() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		final Widgets wid = new Widgets();
		response.setCharacterEncoding(wid.UIcodeSet);
		response.setContentType("application/text");
		
		final int reqId = Integer.parseInt( request.getParameter("reqId") );
		final String modId = request.getParameter("modId");
		final int jobSN = Integer.parseInt( request.getParameter("jobSN"));
		final boolean isDailyBuild = Boolean.parseBoolean( request.getParameter("isDailyBuild") );
		String res ="0";
		final String strSQL ="SELECT testTaskStatusId FROM mptestresultwithreqs WHERE isDailyBuild=? AND jobSN=? AND modId=? AND reqId=? ORDER BY cDT DESC";

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = wid.getConn();
			ps = conn.prepareStatement(strSQL);
			ps.setString(1, isDailyBuild?"t":"f");
			ps.setInt(2, jobSN);
			ps.setString(3,modId);
			ps.setInt(4, reqId);
			rs = ps.executeQuery();
			if ( rs.next() ) {
				res = rs.getInt("testTaskStatusId")+"";
			}
		} catch (Exception e) {
			wid.errLog(e);
		} finally {
			try {
				if ( rs !=null ) {rs.close();}
				if ( ps !=null ) {ps.close();}
				if ( conn !=null ) {conn.close();}	
			} catch (Exception e) { e.printStackTrace(); }
		}
		response.getWriter().write(res);
	}

}
